﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UIElements;
using UnityEngine.UI;
using Button = UnityEngine.UI.Button;
using Image = UnityEngine.UI.Image;

public class StartButton : MonoBehaviour
{
    // Start is called before the first frame update
    public GameObject m_Ready, m_GameOver;
    private float lifeTime = 3f;
    private const float BASESPED = 3f;
    private float spd = 3f;
    public GameObject playerpl;
    private bool ISDISAPPEAR = false, ISREADY = false, DOWN = false;
    public static bool ISSTART = false, ISOVER = false, ISNEXT = false;
    private static bool CANDESTROY = false;
    private float timeint = 0;
    private GameControl gc;
    private Vector2 basepos;
    private Image[] imgSet;
    private Text[] _texts;

    private void Start()
    {
        spd = BASESPED;
        timeint = 0;
        m_Ready = GameObject.Find("Ready");
        m_GameOver = GameObject.Find("GameOver");
        playerpl = GameObject.Find("PlayerPlane");
        gc = GameObject.Find("GameControllor").GetComponent<GameControl>();
        imgSet = playerpl.GetComponentsInChildren<Image>();
        _texts = playerpl.GetComponentsInChildren<Text>();
        m_Ready.SetActive(false);
        playerpl.SetActive(false);
    }

    private void Update()
    {
        if (ISSTART || ISOVER || ISNEXT)
        {
            GetComponent<Button>().enabled = false;
            if (ISSTART) GameStart();
            if (ISOVER) GameOver();
            if (ISNEXT) GameNext();
        }
        else
        {
            GetComponent<Button>().enabled = true;
        }
    }

    public void Click()
    {
        spd = BASESPED;
        ISSTART = true;
        m_Ready.SetActive(true);
        playerpl.SetActive(true);
        DOWN = true;
        StartCoroutine(gc.GameStart());
    }

    IEnumerator Wait(float t, int a)
    {
        yield return new WaitForSeconds(t);
        switch (a)
        {
            case 0:
                ISREADY = true;
                break;
            case 1:
                gameObject.SetActive(false);
                break;
        }
    }

    private void GameStart()
    {
        if (DOWN)
        {
            Slid(-1, 6.0f);
            timeint += Time.deltaTime;
            if (timeint > 0.35)
            {
                DOWN = !DOWN;
                timeint = 0;
                ISDISAPPEAR = true;
            }
        }

        if (ISDISAPPEAR)
        {
            Slid(1, 6.0f);

            foreach (var img in imgSet)
            {
                UIControl.ComeToDisTransparent(img, 1);
            }

            foreach (var text in _texts)
            {
                UIControl.ComeToDisTransparent(text, 1);
            }

            if (UIControl.ComeToDisTransparent(m_Ready.GetComponent<Image>(), 1) &&
                GetComponent<RectTransform>().offsetMax.y > 500)
            {
                StartCoroutine(Wait(2, 0));
                CANDESTROY = false;
                ISDISAPPEAR = false;
            }
        }

        if (ISREADY)
        {
            if (UIControl.ComeToTransparent(m_Ready.GetComponent<Image>(), 10))
            {
                StartCoroutine(Wait(0.1f, 1));
                GameControl.is_pause = false;
                ISREADY = false;
                ISSTART = false;
            }
        }
    }

    private void GameOver()
    {
        if (GetComponent<RectTransform>().offsetMax.y <= 1f)
        {
            GetComponent<RectTransform>().offsetMax = new Vector2(GetComponent<RectTransform>().offsetMax.x,0);
            CANDESTROY = true;
            ISOVER = false;
            m_Ready.SetActive(false);
            playerpl.SetActive(false);
            return;
        }
        Slid(-1, 3f);
        foreach (var img in imgSet)
        {
            UIControl.ComeToTransparent(img, 10);
        }

        foreach (var text in _texts)
        {
            UIControl.ComeToTransparent(text, 10);
        }
    }

    private void GameNext()
    {
        ISOVER = true;
        timeint += Time.deltaTime;
        if (timeint > 5f)
        {
            ISOVER = false;
            timeint = 0;
            ISNEXT = false;
            Click();
        }
    }

    private void Slid(int dirct, float a)
    {
        this.transform.Translate(0, dirct * (spd += a) * Time.deltaTime, 0);
    }

    public static bool Candestroy => CANDESTROY;
}